<?php
/**
 * 消费管理控制器
 * create by liuqi
 */
namespace dg\Controller;
use Think\Controller;

class ConsumeController extends ApibaseController {
	/**
	 * 消费列表，后台管理端
	 */
	public function index()
	{
		$page = I('get.page',1);
		$perpage = 10;
		$url = [];
		$list = M('OrderEntity oe')
			->join('pf_project_entity pe ON oe.project_id = pe.project_id','LEFT')
			->field('oe.id,oe.order_id,oe.user_id,oe.user_name,oe.user_mobile,oe.is_used,oe.score_type,oe.consumer,oe.consumer_mobile,oe.class_room,oe.servicer,oe.counselor,oe.operate_nurse,oe.operate_place,oe.add_at,oe.use_at,pe.project_name,pe.cover_img,pe.project_id');

		if(trim(I('get.user_name'))){
			$list = $list->where(['oe.user_name'=>trim(I('get.user_name'))]);
			$url['user_name'] = trim(I('get.user_name'));
		}
		if(trim(I('get.user_mobile'))){
			$list = $list->where(['oe.user_mobile'=>trim(I('get.user_mobile'))]);
			$url['user_mobile'] = trim(I('get.user_mobile'));
		}
		if(trim(I('get.order_id'))){
			$list = $list->where(['oe.order_id'=>trim(I('get.order_id'))]);
			$url['order_id'] = trim(I('get.order_id'));
		}
		if(in_array(I('get.is_used'),[1,2])){
			$list = $list->where(['oe.is_used'=>I('get.is_used')-1]);
			$url['is_used'] = trim(I('get.is_used'));
		}
		$getObj = clone $list;
		$getData = $list->order(['oe.add_at'=>'desc'])->page($page,$perpage)->select();
		$this->assign('page',setPage($getObj->count(),$perpage,$page,'?'.http_build_query($url)));
		$this->assign('getData',$getData);
		$this->display('consume/index');
	}

	/**
	 * 卡券消费录入
	 */
	public function useCard()
	{
		if(I('post.dosubmit')){
			if(I('post.id')){
				$orderInfo = M('OrderEntity')->where(['id'=>I('post.id')])->find();
				if($orderInfo){
					$set = M('OrderEntity')->where(['id'=>I('post.id'),'is_used'=>0])->setField(['is_used'=>1,'consumer'=>I('post.consumer'),'consumer_mobile'=>I('post.consumer_mobile'),'update_at'=>time(),'use_at'=>strtotime(I('post.utime'))]);
					if($set){
						$this->showtips('使用成功');
					}else{
						$this->showtips('使用失败');
					}
				}else{
					$this->showtips('非法参数');
				}
			}
		}
		if(I('get.id')){
			$this->assign('id',I('get.id'));
			$this->display('consume/use_card');
		}
	}
}